<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        // $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    
    	$this->display();
    }

    public function aaa()
    {
        $this->success('asda','/',1200);
    }

    public function information(){
        // var_dump($_FILES);
    	// var_dump($_POST);die;
    	// $data['pic'] = $_POST['pic'];


    	$data['username'] = !empty($_POST['username']) ? $_POST['username']: redirect('/Home/index', 3, '非法操作,页面跳转中...');
    	$data['phone'] = !empty($_POST['phone']) ? $_POST['phone'] : redirect('/Home/index', 3, '非法操作,页面跳转中...');
    	$data['address'] = !empty($_POST['street']) ? $_POST['pca'].'/'.htmlspecialchars($_POST['street']): redirect('/Home/index', 3, '页面跳转中...');
    	$data['number'] = !empty($_POST['number']) ? $_POST['number'] : redirect('/Home/index', 3, '页面跳转中...');
    	$data['need'] = !empty($_POST['need']) ? htmlspecialchars($_POST['need']) : redirect('/Home/index', 3, '非法操作,页面跳转中...');
        $data['photo'] = !empty($_POST['pic']) ? $this->upload() : '';
        $data['ctime'] = date('Y-m-d H:i:s',time());

    	// $this->upload();die;
        // var_dump($data);die;
    	$user = M('main');
    	$res = $user->data($data)->add();
    	
        if($res){
    	// if(true){
    		// var_dump($_SERVER['HTTP_REFERER']);
    		$this->success($data['username'],'/',3);
    	}else{
    		$this->error('非法操作，添加失败！');
    	}

    }

    //文件上传
    public function upload(){    
	    $upload = new \Think\Upload();// 实例化上传类    
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小    
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型    
	    $upload->rootPath  =      './Statics'; // 设置上传文件根目录
	    $upload->savePath  =      '/Uploads/'; // 设置附件上传目录
	    // $upload->saveExt='png';//上传文件的保存后缀，不设置的话使用原文件后缀
	    // 上传文件     
	    $info   =   $upload->upload();    
	    
        // var_dump($info);die;

        if(!$info) {// 上传错误提示错误信息        
	    	$this->error($upload->getError());    
	    }else{
	    	// 上传成功        
    		// $this->success('上传成功！');   
    		// var_dump($info);//上传文件信息
            $str = '';
            foreach ($info as $k => $v) {
                $str .= $v['savepath'].$v['savename'].'*';
            }
            $new = rtrim($str,'*');//echo $new;die;
            // $res = explode('*',$new);var_dump($res);die;
            return $new;
            //多文件上传时暂时弃用
            // return $info['photo']['savepath'].$info['photo']['savename'];
    	}}


}